package com.jinzheng.qhwocket.commont;

import com.jinzheng.qhwocket.util.GBT32960Decoder;
import com.jinzheng.qhwocket.util.HexUtils;
import io.netty.buffer.Unpooled;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.SimpleChannelInboundHandler;

import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

public class ServerHandler extends SimpleChannelInboundHandler<String> {

    @Override
    protected void channelRead0(ChannelHandlerContext ctx, String data) throws Exception {
        //TODO自己的业务逻辑
        GBT32960Decoder gbt32960Decoder = new GBT32960Decoder();
        byte[] fromHexString = HexUtils.fromHexString(data);
        List<Object> out = new ArrayList<>();
        gbt32960Decoder.decode(ctx, Unpooled.copiedBuffer(fromHexString),out);
        ctx.channel().writeAndFlush("from server: " + UUID.randomUUID());
    }

    @Override
    public void channelInactive(ChannelHandlerContext ctx) throws Exception {
        updateState(ctx);
    }

    @Override
    public void userEventTriggered(ChannelHandlerContext ctx, Object evt) throws Exception {
        updateState(ctx);
    }

    /**
     * @param ctx
     */
    private void updateState(ChannelHandlerContext ctx) {
        //TODO业务逻辑
    }

}
